import { ref } from 'vue';

// 声明uni类型
declare const uni: any;

export default function useSystemInfo() {
  const statusBarHeight = ref(0);
  const headerHeight = ref(0);
  const safeAreaInsetBottom = ref(0);

  try {
    // 使用新的API获取系统信息
    let systemInfo;
    try {
      // 优先使用新API
      if (typeof uni.getWindowInfo === 'function') {
        systemInfo = uni.getWindowInfo();
      } else {
        systemInfo = uni.getSystemInfoSync();
      }
    } catch (e) {
      systemInfo = uni.getSystemInfoSync();
    }
    
    statusBarHeight.value = systemInfo.statusBarHeight || 0;
    
    // 获取底部安全区域高度
    safeAreaInsetBottom.value = systemInfo.safeAreaInsets?.bottom || 0;
    
    // 安全地获取胶囊按钮位置信息
    let menuInfo = null;
    try {
      menuInfo = uni.getMenuButtonBoundingClientRect();
    } catch (e) {
      console.error('获取胶囊按钮信息失败:', e);
    }
    
    // 计算头部高度（状态栏 + 胶囊按钮）
    if (menuInfo && menuInfo.bottom) {
      headerHeight.value = menuInfo.bottom + 10; // 额外加10px的间距
    } else {
      // 如果获取不到胶囊信息，使用状态栏高度加上估计值
      headerHeight.value = (statusBarHeight.value || 0) + 50;
    }
  } catch (error) {
    console.error('获取系统信息失败:', error);
    // 设置默认值
    statusBarHeight.value = 20;
    headerHeight.value = 70;
    safeAreaInsetBottom.value = 0;
  }

  return {
    statusBarHeight,
    headerHeight,
    safeAreaInsetBottom
  };
} 